#Replication files for "Women Running for Office Are Less Risk Averse Than Men. Evidence From Portugal"
#Pedro Magalhães and Miguel M. Pereira
#July 2023

library(stargazer); library(interplot)

setwd('PATH TO DATA FOLDER HERE')
risk<-read.csv('risk_aversion.csv',stringsAsFactors = F)


# # # # OUTCOME VARIABLES
#Holdout
risk$pub_holdout<- ifelse(risk$pub_investment==6,1,0)
risk$priv_holdout<- ifelse(risk$priv_investment==6,1,0)
#Combining both versions
risk$hout<-ifelse(is.na(risk$pub_holdout),risk$priv_holdout,risk$pub_holdout)

#Risk level
risk$pub_risk_level<- ifelse(risk$pub_holdout==1,0,6-risk$pub_investment)
risk$priv_risk_level<- ifelse(risk$priv_holdout==1,0,6-risk$priv_investment)
#Combining both versions
risk$rl<-ifelse(is.na(risk$pub_risk_level),risk$priv_risk_level,risk$pub_risk_level)

#Treatment variables
risk$treatment_priv<-ifelse(is.na(risk$pub_risk_level),1,0)
risk$treatment_priv<-ifelse(is.na(risk$hout),NA,risk$treatment_priv)
risk$treatment_pub<-ifelse(is.na(risk$priv_risk_level),1,0)
risk$treatment_pub<-ifelse(is.na(risk$hout),NA,risk$treatment_pub)

# # # # # 
# Main Models
# # # # # 

#Outcome: Holdout
hout<-lm(hout~mulher+treatment_priv+tenure+educ+incumbent+as.factor(party),risk)
holdout_unadj<-lm(hout~mulher+treatment_priv,risk)

#Outcome: Risk Level
rl<-lm(rl~mulher+treatment_priv+tenure+educ+incumbent+as.factor(party),risk)
pooledrisk<-lm(rl~mulher+treatment_priv,risk)

###############################################
#Table 1 (and Table C1): Gender differences in Risk Aversion
###############################################
spce<-lm(rl~1,risk)
stargazer(hout,spce,rl,no.space = T,
          covariate.labels = c('Woman','Personal investment',
                               'Experience in office', 'Education',
                               'Incumbent'))

###############################################
#Figure 1: Gender differences in Risk Aversion by incumbency status
###############################################
hout_inc<-lm(hout~mulher*incumbent+treatment_priv+tenure+educ+as.factor(party),risk)
rl_inc<-lm(rl~mulher*incumbent+treatment_priv+tenure+educ+as.factor(party),risk)

set.seed(789057)
hout_plot<-interplot(hout_inc,'mulher','incumbent')
hout_plot90<-interplot(hout_inc,'mulher','incumbent',ci=.9)
rl_plot<-interplot(rl_inc,'mulher','incumbent')
rl_plot90<-interplot(rl_inc,'mulher','incumbent',ci=.9)

# # # # Figure 1: PANEL A
dev.off()
par(mar=c(3,5,2,2))
plot(c(1,5),c(-.35,.05),type='n',xaxt='n',xlab='',
     ylab='Marginal effect of Woman\non probability of holdout', yaxt='n')
grid()
abline(0,0)
axis(1,c(2,4), labels = c('Not incumbent', 'Incumbent'),
     cex.axis=1, padj=.4)
axis(2,c(-.3,-.2,-.1,0), cex.axis=1)
points(c(2,4),hout_plot$data$coef1,pch=20, col='aquamarine4', cex=2)
segments(2,hout_plot$data$lb[1], 2,hout_plot$data$ub[1], col='aquamarine4')
segments(4,hout_plot$data$lb[2], 4,hout_plot$data$ub[2], col='aquamarine4')
segments(2,hout_plot90$data$lb[1], 2,hout_plot90$data$ub[1], col='aquamarine4',lwd=2)
segments(4,hout_plot90$data$lb[2], 4,hout_plot90$data$ub[2], col='aquamarine4',lwd=2)


# # # # Figure 1: PANEL B
dev.off()
par(mar=c(3,5,2,2))
plot(c(1,5),c(-.1,.9),type='n',xaxt='n',xlab='',
     ylab='Marginal effect of Woman\non risk level', yaxt='n')
grid()
abline(0,0)
axis(1,c(2,4), labels = c('Not incumbent', 'Incumbent'),
     cex.axis=1, padj=.4)
axis(2,c(0,.2,.4,.6,.8), cex.axis=1)
points(c(2,4),rl_plot$data$coef1,pch=20, col='aquamarine4', cex=2)
segments(2,rl_plot$data$lb[1], 2,rl_plot$data$ub[1], col='aquamarine4')
segments(4,rl_plot$data$lb[2], 4,rl_plot$data$ub[2], col='aquamarine4')
segments(2,rl_plot90$data$lb[1], 2,rl_plot90$data$ub[1], col='aquamarine4',lwd=2)
segments(4,rl_plot90$data$lb[2], 4,rl_plot90$data$ub[2], col='aquamarine4',lwd=2)

###############################################
#Table B1: Unadjusted gender differences in Risk Aversion
###############################################

#Age
summary(risk$idade)
#Gender
prop.table(table(risk$mulher))
#Education: College degree +
risk$college_over<-ifelse(risk$educ>=7,1,0)
prop.table(table(risk$college_over))
#Previously elected to parliament
prop.table(table(risk$incumbent))
#Anos no parlamento
summary(risk$tenure)
#Partido
prop.table(table(risk$party))




###############################################
#Table C1: Unadjusted gender differences in Risk Aversion
###############################################
stargazer(hout,spce,rl,no.space = T,
          covariate.labels = c('Woman','Personal investment',
                               'Experience in office', 'Education',
                               'Incumbent'))

###############################################
#Table C2: Unadjusted gender differences in Risk Aversion
###############################################
stargazer(holdout_unadj,spce,pooledrisk,no.space = T,
          covariate.labels = c('Woman','Personal investment'))

###############################################
#Table C3: Main models accounting for candidates age
###############################################
hout_age<-lm(hout~mulher+treatment_priv+tenure+educ+incumbent+idade+as.factor(party),risk)
rl_age<-lm(rl~mulher+treatment_priv+tenure+educ+incumbent+idade+as.factor(party),risk)

stargazer(hout_age,spce,rl_age,no.space = T,
          covariate.labels = c('Woman','Personal investment',
                               'Experience in office', 'Education',
                               'Incumbent','Age'))

# # # CONSIDER DROPPING THIS C4

###############################################
#Table C4: Effects of candidates' gender and type of investment on risk preferences, excluding zero risk
###############################################
rl0<-lm(rl~mulher+treatment_priv+tenure+educ+incumbent+as.factor(party),risk[risk$rl!=0,])

stargazer(rl0,no.space = T,
          covariate.labels = c('Woman','Personal investment',
                               'Experience in office', 'Education',
                               'Incumbent','Age'))


###############################################
#Table C5: Effects of candidates' gender on risk attitudes, by incumbency status
###############################################
stargazer(hout_inc,rl_inc,no.space = T,
          covariate.labels = c('Woman','Incumbent','Personal investment',
                               'Experience in office', 'Education'))


###############################################
#Table C6: Effects of candidates' gender on risk attitudes, by time in office
###############################################
summary(hout_tenure<-lm(hout~mulher*tenure+treatment_priv+educ+incumbent+as.factor(party),risk))
summary(rl_tenure<-lm(rl~mulher*tenure+treatment_priv+educ+incumbent+as.factor(party),risk))

stargazer(hout_tenure,rl_tenure,no.space = T,
          covariate.labels = c('Woman','Personal investment',
                               'Experience in office', 'Education',
                               'Incumbent'))

